package com.zsrt.devbase.common.utils;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


public class StringUtil {
	
	final static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8',  
            '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',  
            'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',  
            'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',  
            'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',  
            'Z' };  
  
    final static Map<Character, Integer> digitMap = new HashMap<Character, Integer>();  
  
    static {  
        for (int i = 0; i < digits.length; i++) {  
            digitMap.put(digits[i], (int) i);  
        }  
    }  
  
    /** 
     * 支持的最大进制数 
     */  
    public static final int MAX_RADIX = digits.length;  
  
    /** 
     * 支持的最小进制数 
     */  
    public static final int MIN_RADIX = 2; 
	
	public static boolean isEmpty(String value){
		return null==value || "".equals(value);
	}
	public static boolean isBlank(String value){
		return null==value || "".equals(value.trim());
	}
	/**
	 * 将字符串统一转UTF-8
	 * @param content
	 * @return
	 */
	public static String isoToUTF8(String content){
		try {
			if("GB2312".equals(getEncoding(content)))
			{
				return content;
			}
			return new String(content.getBytes(getEncoding(content)),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	/**
	 * 
	 * @param str
	 * @return
	 */
    public static String getEncoding(String str) {      
        String encode = "GB2312";      
       try {      
           if (str.equals(new String(str.getBytes(encode), encode))) {      
                String s = encode;      
               return s;      
            }      
        } catch (Exception exception) {      
        }      
        encode = "ISO-8859-1";      
       try {      
           if (str.equals(new String(str.getBytes(encode), encode))) {      
                String s1 = encode;      
               return s1;      
            }      
        } catch (Exception exception1) {      
        }      
        encode = "UTF-8";      
       try {      
           if (str.equals(new String(str.getBytes(encode), encode))) {      
                String s2 = encode;      
               return s2;      
            }      
        } catch (Exception exception2) {      
        }      
        encode = "GBK";      
       try {      
           if (str.equals(new String(str.getBytes(encode), encode))) {      
                String s3 = encode;      
               return s3;      
            }      
        } catch (Exception exception3) {      
        }      
       return "";      
    }
    /**
     * 获取UUID
     * @return
     */
 	public static String getUUID(){
		String uuid[] = UUID.randomUUID().toString().split("-");
		StringBuffer id = new StringBuffer();
		for (String string : uuid) {
			id.append(string);
		}
		return id.toString();
	}
 	
 	
 	
 	/**
 	 * 将前端的datebox时间格式为06/30/2016 09:19 转为2016-06-30 09:19
 	 * @return
 	 */
 	public static String changeDateBoxTime(String boxtime){
 		String[] times = boxtime.split(" ");
 		String[] dates =  times[0].split("/");
 		StringBuffer sb = new StringBuffer();
 		
 		return sb.append(dates[2]+"-"+dates[0]+"-"+dates[1]+" "+times[1]).toString();
 	}
 	
 	/**
 	 * 
 	 * @author leixin
 	 * @createTime 上午9:38:17
 	 * @param strings  带转换字符串数组
 	 * @param str 需要在数组中间加入的间隔字符串
 	 * @return
 	 */
 	public static String join(String[] strings, String str) {
 		StringBuilder result = new StringBuilder();
 		if (strings.length == 0 || null == strings) {
 			return null;
		}else {
			for (int i = 0; i < strings.length - 1; i++) {
				result = result.append(strings[i]+str);
			}
			result = result.append(strings[strings.length - 1]);
		}
		return result.toString();
	}
 	
 	/**
 	 * 取随机的大写字母，拼接成字符串
 	 * @author leixin
 	 * @createTime 下午4:11:20
 	 * @param num 需要生产的随机字母的个数
 	 * @return
 	 */
 	public static String randomAlphabet(int num) {
 		char[] alphabet = new char[num];
 		for (int i = 0; i < num; i++) {
 			int randomNumber = 65 + (int) (Math.random()* 26);
 			alphabet[i] = (char)randomNumber;
		}
 		return String.valueOf(alphabet);
		
	}
 	
 	/**
 	 * 获取请求URI的方法
 	 * 
 	 * @param uri
 	 * @return
 	 */
 	public static String getMethodURI(String uri){
 		String[] methods = uri.split("/");
 		return methods[methods.length-1];
 	}
 	
 	/** 
     * 以62进制（字母加数字）生成19位UUID
     * @return 
     */  
    public static String getShortUUID() {  
        UUID uuid = UUID.randomUUID();  
        StringBuilder sb = new StringBuilder();  
        sb.append(digits(uuid.getMostSignificantBits() >> 32, 8));  
        sb.append(digits(uuid.getMostSignificantBits() >> 16, 4));  
        sb.append(digits(uuid.getMostSignificantBits(), 4));  
        sb.append(digits(uuid.getLeastSignificantBits() >> 48, 4));  
        sb.append(digits(uuid.getLeastSignificantBits(), 12));  
        return sb.toString();  
    }
 	 
  
    /** 
     * 将长整型数值转换为指定的进制数（最大支持62进制，字母数字已经用尽） 
     *  
     * @param i 
     * @param radix 
     * @return 
     */  
    private static String toString(long i, int radix) {  
        if (radix < MIN_RADIX || radix > MAX_RADIX)  
            radix = 10;  
        if (radix == 10)  
            return Long.toString(i);  
  
        final int size = 65;  
        int charPos = 64;  
  
        char[] buf = new char[size];  
        boolean negative = (i < 0);  
  
        if (!negative) {  
            i = -i;  
        }  
  
        while (i <= -radix) {  
            buf[charPos--] = digits[(int) (-(i % radix))];  
            i = i / radix;  
        }  
        buf[charPos] = digits[(int) (-i)];  
  
        if (negative) {  
            buf[--charPos] = '-';  
        }  
  
        return new String(buf, charPos, (size - charPos));  
    }  
  
    private static String digits(long val, int digits) {  
        long hi = 1L << (digits * 4);  
        return toString(hi | (val & (hi - 1)), MAX_RADIX).substring(1);  
    }  
 	
}
